package khannedy.blogging.masterdetail.view;

import java.awt.Image;
import java.util.Date;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import khannedy.blogging.masterdetail.entity.Mahasiswa;
import khannedy.blogging.masterdetail.entity.Mahasiswa.JenisKelamin;
import khannedy.blogging.masterdetail.entity.MataKuliah;
import khannedy.blogging.masterdetail.model.TableModelMahasiswa;
import khannedy.blogging.masterdetail.model.TableModelMataKuliah;
import khannedy.blogging.masterdetail.service.MahasiswaService;
import org.jasoet.swing.cellrenderer.DateTableCellRenderer;
import org.jasoet.swing.cellrenderer.DoubleCellRenderer;
import org.jasoet.swing.cellrenderer.ImageTableCellRenderer;
import org.jasoet.swing.cellrenderer.JenisKelaminTableCellRenderer;

public class Form extends javax.swing.JFrame {

    private MahasiswaService service;
    private TableModelMahasiswa modelMahasiswa;
    private TableModelMataKuliah modelMataKuliah;

    /** Creates new form Form */
    public Form() {
        initComponents();

        service = new MahasiswaService();
        modelMahasiswa = new TableModelMahasiswa(service.getAllMahasiswa(100));
        modelMataKuliah = new TableModelMataKuliah();

        //Added by Jasoet [start]
        tabelMahasiswa.setDefaultRenderer(Image.class, new ImageTableCellRenderer());
        tabelMahasiswa.setDefaultRenderer(Date.class, new DateTableCellRenderer());
        tabelMahasiswa.setDefaultRenderer(JenisKelamin.class, new JenisKelaminTableCellRenderer());
        tabelMataKuliah.setDefaultRenderer(Double.class, new DoubleCellRenderer());
        //Added by Jasoet [end]

        
        tabelMahasiswa.setModel(modelMahasiswa);
        tabelMataKuliah.setModel(modelMataKuliah);



        tabelMahasiswa.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                int selected = tabelMahasiswa.getSelectedRow();
                if (selected != -1) {
                    Mahasiswa mahasiswa = modelMahasiswa.getList().get(selected);
                    List<MataKuliah> list = mahasiswa.getMataKuliahs();
                    modelMataKuliah.setList(list);
                }
            }
        });

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabelMahasiswa = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelMataKuliah = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Master Detail");

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        tabelMahasiswa.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(tabelMahasiswa);

        jLabel1.setText("Data Mahasiswa :");

        jLabel2.setText("Mata Kuliah yang Diambil :");

        jScrollPane2.setViewportView(tabelMataKuliah);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tabelMahasiswa;
    private javax.swing.JTable tabelMataKuliah;
    // End of variables declaration//GEN-END:variables
}
